package com.cszy.ewmxns.base



import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.fragment.app.Fragment

import androidx.lifecycle.Lifecycle
import androidx.viewbinding.ViewBinding


import com.cszy.yydqbfq.common.EventUtils




import java.lang.reflect.ParameterizedType

abstract class BaseFragment<VB : ViewBinding, T> : Fragment() {

    private var _binding: VB? = null

    protected val mBinding get() = _binding!!

    protected var mActivity: BaseActivity<*, Any?>? = null

    private var isFirst = true

    @LayoutRes
    protected open val layoutId = 0

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mActivity = getBaseActivity()
    }

    open fun getBaseActivity(): BaseActivity<*, Any?>? {
        return this.activity as BaseActivity<*, Any?>?
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        if (isRegisterEvent()) {
            EventUtils.register(this)
        }
        return initBinding(inflater, container)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        onVisible()
        initView(savedInstanceState)
        initObserve()
        initData()
        initListener()
    }

    abstract fun initView(savedInstanceState: Bundle?)

    open fun initData() {}

    open fun initListener() {}

    open fun initObserve() {}

    override fun onResume() {
        super.onResume()
        onVisible()
    }

    private fun onVisible() {
        if (lifecycle.currentState == Lifecycle.State.STARTED && isFirst) {
            lazyLoad()
            isFirst = false
        }
    }

    open fun lazyLoad() {}

    protected open fun isRegisterEvent(): Boolean {
        return false
    }



    open fun handleEvent(event: BaseEvent) {}

    open fun handleStickyEvent(event: BaseEvent) {}

    protected fun showLoading(text: String) {
        getBaseActivity()?.showLoading(text)
    }

    protected fun dismissLoading() {
        getBaseActivity()?.dismissLoading()
    }

    private fun initBinding(inflater: LayoutInflater, container: ViewGroup?): View {
        val type = javaClass.genericSuperclass
        if (type is ParameterizedType) {
            val cls = type.actualTypeArguments[1] as Class<*>
            return when {
                ViewBinding::class.java.isAssignableFrom(cls) && cls != ViewBinding::class.java -> {
                    cls.getDeclaredMethod("inflate", LayoutInflater::class.java).let {
                        @Suppress("UNCHECKED_CAST") _binding = it.invoke(null, inflater) as VB
                        mBinding.root
                    }
                }
                else -> {
                    if (layoutId == 0) throw IllegalArgumentException("If you don't use ViewBinding, you need to override method layoutId")
                    inflater.inflate(layoutId, container, false)
                }
            }
        } else throw IllegalArgumentException("Generic error")
    }

    override fun onDestroy() {
        super.onDestroy()
        if (isRegisterEvent()) {
            EventUtils.unregister(this)
        }
    }

}